package cn.ainit.yz.cloutStock;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ainit.common.ReturnObject;
import cn.ainit.sys.utils.UUID32;
import cn.ainit.yz.cloutStock.entity.CloutStock;
import cn.ainit.yz.cloutStock.entity.CloutStockItem;
import cn.ainit.yz.cloutStock.entity.CloutStockObj;

@Controller
@RequestMapping("/cloutStock")
public class CloutStockController {
	private CloutStockService cloutStockService;
	@Resource
	public void setCloutStockService(CloutStockService cloutStockService) {
		this.cloutStockService = cloutStockService;
	}
	
	@RequestMapping("/saveCloutStock")
	@ResponseBody
	public ReturnObject saveCloutStock(@RequestBody CloutStockObj cloutStockObj, HttpServletRequest request){
		ReturnObject returnObject = new ReturnObject();
		CloutStock outStock= cloutStockObj.getCloutStock();
		List<CloutStockItem> cloutStockItems = cloutStockObj.getCloutStockItems();

		try {
			if (outStock.getOutId() != null && !outStock.getOutId().equals("")) {
				//删除旧的不存在的记录    条件  sp_id 和 sp_item_id
				if(cloutStockService.updCloutStock(outStock, cloutStockItems)){
					returnObject.setMsg("upsuccess");
				}else{
					returnObject.setMsg("upfail");
				}
				
			}else{
				String sprkId = UUID32.newGuid();
				outStock.setOutId(sprkId);
				//保存操作
				if(cloutStockService.saveCloutStock(outStock, cloutStockItems)){
					returnObject.setMsg("success");
					returnObject.setInfo(sprkId);
				}else{
					returnObject.setMsg("fail");
				}
			}
		} catch (Exception e) {
			returnObject.setMsg(e.getMessage());
			e.printStackTrace();
		}
		return returnObject;
	}
}
